<?php

/**
 * This is the model class for table "{{question}}".
 *
 * The followings are the available columns in table '{{question}}':
 * @property string $question
 * @property string $title
 * @property string $answer_a
 * @property string $answer_b
 * @property string $answer_c
 * @property string $answer_d
 * @property string $answer
 * @property string $bank_id
 *
 * The followings are the available model relations:
 * @property QuestionBank $bank
 */
class Question extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{question}}';
	}

  public function defaultScope()
  {
    return array(
      'alias'=>'question'
    );
  }


  /**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, answer, bank_id', 'required'),
			array('title', 'length', 'max'=>256),
			array('answer_a, answer_b, answer_c, answer_d', 'length', 'max'=>128),
			array('answer', 'length', 'max'=>1),
			array('bank_id', 'length', 'max'=>10),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('question, title, answer_a, answer_b, answer_c, answer_d, answer, bank_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bank' => array(self::BELONGS_TO, 'QuestionBank', 'bank_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'question' => 'Question',
			'title' => '题目名称',
			'answer_a' => '答案A',
			'answer_b' => '答案B',
			'answer_c' => '答案C',
			'answer_d' => '答案D',
			'answer' => '正确答案',
			'bank_id' => '所属题库',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('question',$this->question,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('answer_a',$this->answer_a,true);
		$criteria->compare('answer_b',$this->answer_b,true);
		$criteria->compare('answer_c',$this->answer_c,true);
		$criteria->compare('answer_d',$this->answer_d,true);
		$criteria->compare('answer',$this->answer,true);
		$criteria->compare('bank_id',$this->bank_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Question the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
